package com.college.eduservice.controller;


import com.college.commonutils.R;
import com.college.eduservice.client.VodClient;
import com.college.eduservice.entity.EduVideo;
import com.college.eduservice.service.EduVideoService;
import com.college.servicebase.exceptionhandler.CollegeException;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-20
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    //注入vodclient
    @Autowired
    private VodClient vodClient;

    @Autowired
    private EduVideoService videoService;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节,删除阿里云对应的视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {
        //根据小节id获取视频id，在调用方法
        EduVideo video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();

        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id远程调用进行删除
            R r = vodClient.removeAlyVideo(videoSourceId);
            if (r.getCode() == 20001){
                throw new CollegeException(20001,"删除视频失败，熔断器。。。。");
            }
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //查询小节id
    @GetMapping("/getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id){
        EduVideo eduVideo = videoService.getById(id);
        return R.ok().data("eduVideo",eduVideo);
    }
    
    //修改小节
    @PostMapping("/updateVideoInfo")
    public R updateVideoInfo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }
    
}

